﻿Imports log4net
Imports Equipment.DataAccess

Partial Public Class AssetRequirement

    Private Shared logger As ILog = LogManager.GetLogger(GetType(AssetRequirement))

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property

    Private _groupList As List(Of Group)
    Public Property Groups As List(Of Group)
        Get
            If _groupList Is Nothing Then

                _groupList = New List(Of Group)

                For Each groupItem In (From r In Database.Instance.AssetRequirementGroups Where r.AssetRequirementID = Me.ID Select r.Group)
                    _groupList.Add(groupItem)
                Next

            End If
            Return _groupList
        End Get
        Set(ByVal value As List(Of Group))
            _groupList = value
        End Set
    End Property

    Private _reviewGroupList As List(Of Group)
    Public Property ReviewGroups As List(Of Group)
        Get
            If _reviewGroupList Is Nothing Then

                _reviewGroupList = New List(Of Group)

                For Each groupItem In (From r In Database.Instance.AssetRequirementRevewGroups Where r.AssetRequirementID = Me.ID Select r.Group)
                    _reviewGroupList.Add(groupItem)
                Next

            End If
            Return _reviewGroupList
        End Get
        Set(ByVal value As List(Of Group))
            _reviewGroupList = value
        End Set
    End Property
#End Region

#Region "Public Section"
    Public Shared Function GetBy(ByVal assetID As Integer, ByVal reqId As Integer) As AssetRequirement
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From ar In db.AssetRequirements Where ar.AssetID = assetID AndAlso ar.RequirementID = reqId
        Dim assetRequirementEntity As AssetRequirement = result.FirstOrDefault()
        If assetRequirementEntity IsNot Nothing Then
            assetRequirementEntity.DBContext = db
        End If
        Return assetRequirementEntity
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As AssetRequirement
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From ar In db.AssetRequirements Where ar.ID = id
        Dim assetRequirementEntity As AssetRequirement = result.FirstOrDefault()
        assetRequirementEntity.DBContext = db
        Return assetRequirementEntity
    End Function

    Public Sub UpdateStartDate(ByVal startDate As DateTime)
        Dim ar = AssetRequirement.GetByPrimaryKey(Me.ID)
        ar.StartDate = startDate
        ar.Save()
    End Sub

    Public Sub Update(ByVal startDate As DateTime, ByVal reviewType As RequirementReviewType, ByVal groups As List(Of Group), ByVal reviewGroups As List(Of Group))
        Dim ar = AssetRequirement.GetByPrimaryKey(Me.ID)
        Dim _dataBase As EquipmentDataContext = ar.DBContext
        ar.StartDate = startDate
        ar.ReviewType = reviewType

        ' Groups
        For Each groupItem In ar.AssetRequirementGroups
            Dim currGroup As Group = groups.SingleOrDefault(Function(g) g.ID = groupItem.GroupID)
            If currGroup Is Nothing Then
                _dataBase.AssetRequirementGroups.DeleteOnSubmit(groupItem)
            End If
        Next
        For Each groupItem In groups
            Dim currGroup As Group = ar.Groups.SingleOrDefault(Function(g) g.ID = groupItem.ID)
            If currGroup Is Nothing Then
                Dim newReqGroup As New AssetRequirementGroup
                newReqGroup.AssetRequirementID = Me.ID
                newReqGroup.GroupID = groupItem.ID
                _dataBase.AssetRequirementGroups.InsertOnSubmit(newReqGroup)
            End If
        Next

        ' Review Groups
        For Each groupItem In ar.AssetRequirementRevewGroups
            Dim currGroup As Group = reviewGroups.SingleOrDefault(Function(g) g.ID = groupItem.GroupID)
            If currGroup Is Nothing Then
                _dataBase.AssetRequirementRevewGroups.DeleteOnSubmit(groupItem)
            End If
        Next
        For Each groupItem In reviewGroups
            Dim currGroup As Group = ar.ReviewGroups.SingleOrDefault(Function(g) g.ID = groupItem.ID)
            If currGroup Is Nothing Then
                Dim newReqReviewGroup As New AssetRequirementRevewGroup
                newReqReviewGroup.AssetRequirementID = Me.ID
                newReqReviewGroup.GroupID = groupItem.ID
                _dataBase.AssetRequirementRevewGroups.InsertOnSubmit(newReqReviewGroup)
            End If
        Next

        ar.Save()
    End Sub

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext

            Try
                _dataBase.SubmitChanges()
            Catch ex As Exception
                logger.Error(ex.Message + ":" + ex.StackTrace)
            End Try
        End If

    End Sub
#End Region
End Class
